package com.zzb.servlet.user;


import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.UserVO;
import com.zzb.service.UserService;
import com.zzb.service.impl.UserServiceImpl;
import com.zzb.util.Tool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/user/update")
public class UserUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //得到前端数据
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String address = req.getParameter("address");
        String birthday = req.getParameter("birthday");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String enable = req.getParameter("enable");
        //String remark = req.getParameter("remark");


        //对前端数据进行封装
        UserVO userVO = UserVO.builder()
                .id(Integer.valueOf(id))
                .username(Tool.nullToStr(username))
                .password(password)
                .sex(sex)
                .address(Tool.nullToStr(address))
                .birthday(Tool.nullToStr(birthday))
                .enable(Integer.valueOf(enable))
                .email(Tool.nullToStr(email))
                .phone(Tool.nullToStr(phone))
                //.remark(Tool.nullToStr(remark))
                .build();

        //调用服务逻辑
        UserService userService = new UserServiceImpl();
        //得到响应数据
        ResultDTO resultDTO = userService.update(userVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
